package com.thinkgem.jeesite.common.frame.rpc.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.thinkgem.jeesite.common.frame.rpc.bean.RPCHeader;
import com.thinkgem.jeesite.common.frame.rpc.bean.RPCInData;
import com.thinkgem.jeesite.common.frame.rpc.process.IRPCProcess;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by liufeng on 2016/6/6.
 */
@Controller
@RequestMapping("${webServicePath}")
public class RPCWSServlet {


    //日志对象
    private transient Log log = LogFactory.getLog(this.getClass());

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private IRPCProcess process;

    @ResponseBody
    ///client/shopAdmin/user/addUser?sign=123&time=123&data={}
    ///public/shop/item/addItem?sign=123&time=123&data={}
    @RequestMapping("/{scope}/{sys}/{module}/{method}")
    public String jsonRPC(@PathVariable("scope") String scope, @PathVariable("sys") String sys, @PathVariable("module") String module,
                          @PathVariable("method") String method) {

        Boolean isMultipart = false;

        MultipartHttpServletRequest multipartHttpServletRequest = null;

        //创建通用多部分解析器
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        //设置编码
        commonsMultipartResolver.setDefaultEncoding("UTF-8");
        //判断request是否文件上传，即多部分请求

        if(commonsMultipartResolver.isMultipart(request)){
            isMultipart=true;
            multipartHttpServletRequest = commonsMultipartResolver.resolveMultipart(request);
        }


        //TODO 处理图片上传
        //        if(isMultipart){
//            Iterator<String> fileNames = multipartHttpServletRequest.getFileNames();
//            while (fileNames.hasNext()){
//                String name = fileNames.next();
////                inData.getFileData().put(name,multipartHttpServletRequest.getFile(name));
//                paramMap.put(name,multipartHttpServletRequest.getFile(name));
//
//            }
//
//        }

        String time = request.getParameter("time");
        String sign = request.getParameter("sign");
        String dataString = request.getParameter("data");
        if(StringUtils.isBlank(time)){

        }
        if(StringUtils.isBlank(sign)){

        }
        if(StringUtils.isBlank(dataString)){

        }
        RPCHeader rpcHeader = new RPCHeader.RPCHeaderBuilder().addTime(time).addSign(sign).addMethod(method).addModule(module).addScope(scope).addSys(sys).builder();

        JSONObject data = null;
        try {
            data = JSON.parseObject(dataString);
        }catch (Exception e){
            e.printStackTrace();

        }


        RPCInData inData = new RPCInData.RPCInDataBuilder().addData(data).addRPCHeader(rpcHeader).builder();

        process.process(inData);




        return null;
    }


}
